<?php

namespace app\controller\notify;

use app\exception\PaypalBusinessException;
use app\libs\PaypalSDK\action\PurchasePaypal;
use app\model\Notify;
use app\model\Orders;
use app\model\ShopsPayment;
use app\service\payment\gateway\PaypalService;
use think\helper\Arr;
use think\Request;

class NotifyPaypal implements NotifyInterface
{

    public function notify(Request $request)
    {
        return null;
    }

    public function capture(Request $request, Orders $order)
    {
            $service = new PaypalService();
            $response = $service->getPaypalInfo($order);//详情
            $result = Arr::get($response,"result",[]);
            //快捷支付
            if ($order->is_paypal_kj) {
                list($address, $email) = PaypalService::parseAddresses($result);
                $order->email = $email;
                $order->addresses()->saveAll($address);
                $order->save();
            }
        $paypalStatus = Arr::get($result,'status','');
        if($paypalStatus == 'COMPLETED') throw new PaypalBusinessException("order has completed");
        $service->capture($order);//扣款
    }

}